﻿namespace EyeOpen.Mvc.Mvc.Test
{
	using System;
	using System.Collections.Generic;
	using System.Linq;

	using EyeOpen.Security.Data;
	using EyeOpen.Security.Domain;
	using EyeOpen.Security.Service;
	using EyeOpen.Testing;

	using Microsoft.VisualStudio.TestTools.UnitTesting;

	using Moq;

	using SharpTestsEx;

	[TestClass]
	public class UserLoginServiceTest
	{
		private const string CorrectEmail = "b@b.com";
		private const string WrongEmail = "b@b1.com";

		private const string CorrectPassword = "password";
		private const string WrongPassword = "wrongpassword";

		[TestMethod]
		public void AuthenticateUsingWrongEmailAndWrongPasswordExpectedUserIsNotValid()
		{
			CheckUserCredentials(WrongEmail, WrongPassword, false);
		}

		[TestMethod]
		public void AuthenticateUsingCorrectEmailAndWrongPasswordExpectedUserIsNotValid()
		{
			CheckUserCredentials(CorrectEmail, WrongPassword, false);
		}

		[TestMethod]
		public void AuthenticateUsingWrongEmailAndCorrectPasswordExpectedUserIsNotValid()
		{
			CheckUserCredentials(WrongEmail, CorrectPassword, false);
		}

		[TestMethod]
		public void AuthenticateUsingCorrectEmailAndCorrectPasswordExpectedUserIsValid()
		{
			CheckUserCredentials(CorrectEmail, CorrectPassword, true);
		}

		private void CheckUserCredentials(string email, string password, bool expected)
		{
			new UserLoginService()
				.IsValidUser(email, password, CreateUow())
				.Should()
				.Be
				.EqualTo(expected);
		}

		private ISecurityUnitOfWork CreateUow()
		{
			var uowMock = new Mock<ISecurityUnitOfWork>();

			uowMock
				.SetupGet(x => x.UserRepository)
				.Returns(new RepositoryMock<User>(GetUserList().ToArray()).Object);

			return uowMock.Object;
		}

		private IEnumerable<User> GetUserList()
		{
			yield return
				new User
				{
					Id = Guid.NewGuid(),
					Email = CorrectEmail,
					Password = User.CreatePassword(CorrectPassword)
				};
		}
	}
}